#include "srv_head.h"
/*

	传参查看当路径文件
*/
/*
void do_cli_ls(void)
{
	system("clear");
	DIR *mydir = NULL;
	struct dirent *item = NULL;
	char buf[1024];
	//int i = 1;
	int line = 1;
	//打开目录流对象  
	
	mydir = opendir(".");
	if(NULL == mydir){
		perror("opendir");
		exit(1);
		}
	//2.从目录流对象中循环读取dirent列表项
	
	while((item = readdir(mydir)) != NULL){
		bzero(buf, 1024);
		//printf("line%d:%s\n",i,item->d_name);
		snprintf(buf, 1024, "line %d:%s\n",line++,item->d_name);
		printf("%s",buf);
		
//		send(newfd, buf,strlen(buf),0);
//		printf("%s",buf);
		
		usleep(100000);
	}
	//3关闭目录流对象
	closedir(mydir);
}

void cli_ls(int newfd)
{
	system("clear");
	DIR *mydir = NULL;
	struct dirent *item = NULL;
	char buf[1024];
	//int i = 1;
	int line = 1;
	//打开目录流对象  
	
	mydir = opendir(".");
	if(NULL == mydir){
		perror("opendir");
		exit(1);
		}
	//2.从目录流对象中循环读取dirent列表项
	
	while((item = readdir(mydir)) != NULL){
		bzero(buf, 1024);
		//printf("line%d:%s\n",i,item->d_name);
		snprintf(buf, 1024, "line %d:%s\n",line++,item->d_name);
//		read(newfd, buf, sizeof(buf));
		write(newfd, buf,strlen(buf));  //发送给客户端
		printf("%s",buf);
		usleep(100000);
		
	}
	bzero(buf, 1024);
	write(newfd, "over", strlen("over"));  //发送给客户端
  //发送给客户端
	usleep(100000);

	//3关闭目录流对象
	closedir(mydir);
}
*/


void do_cli_ls(int sockfd)
{
	system("clear");
	char filename[N];
	DIR *dir = NULL;
	struct dirent *item = NULL;
//	int i = 0;
//	while(i != 10) {
//		i++;
//		snprintf(filename, N, "test srv filename %d", i);
//		if(send(sockfd, filename, N, 0) <0 ){
//       	perror("write");
//			return;
//		}
//		printf("****** %s\n", filename);
//	}

//	char filename[1024];
//	int ret; 
	dir = opendir(".");
	if(NULL == dir) {
		perror("opendir");
		exit(1);
	}
	printf(RED "server all file list:\n" NONE);
	
	while ((item = readdir(dir)) != NULL) {
		bzero(filename, N);
		if(snprintf(filename, N, "%s", item->d_name) < 0)
		{
			perror("sprintf");
			exit(1);
		}
		if((strncmp(filename, ".", 1) != 0) && (strncmp(filename, "..", 2) != 0) ){
			if(send(sockfd, filename, N,0) < 0){
				perror("send ls");
				exit(1);
			}
			printf("*** %s\n", filename);
		}
	}
	
	


	closedir(dir);
	close(sockfd);


}






